<template>
  <BasicModal v-bind="$attrs" title="教育实习小组评价" @ok="handleSubmit" @register="registerModal">
    <BasicForm @register="registerForm" />
  </BasicModal>
</template>

<script lang="ts" setup>
  import { ref } from "vue";
  import { BasicModal, useModalInner } from "/@/components/Modal";
  import { BasicForm, useForm } from "/@/components/Form";
  import { useMessage } from "/@/hooks/web/useMessage";
  import { formSchema } from "/@/views/captain/practical/data";
  import { setGroupScore } from "/@/api/core/internInfo";
  import { defaultModalFormSettings } from "/@/hooks/core/useDefault";

  const userId = ref(0);

  const [registerForm, { validate, setFieldsValue }] = useForm({
    schemas: formSchema,
    ...defaultModalFormSettings,
  });

  const [registerModal, { closeModal, changeLoading, changeOkLoading }] = useModalInner((data) => {
    userId.value = data.userId;
    setFieldsValue(data.score);
  });

  const { createMessage } = useMessage();

  async function handleSubmit() {
    const { score, remark } = await validate();
    try {
      changeLoading(true);
      changeOkLoading(true);
      await setGroupScore(userId.value, score, remark);
      createMessage.success("提交成功");
      closeModal();
    } finally {
      changeLoading(false);
      changeOkLoading(false);
    }
  }
</script>
